home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2000 #1 / Amiga Plus CD - 2000 - No. 1.iso / Tools / Dev / Meshwriter / vrml1.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-12-03  |  7.4 KB  |  234 lines

  1. /*
  2. **      $VER: vrml1.c 1.00 (27.03.1999)
  3. **
  4. **      Creation date : 29.11.1998
  5. **
  6. **      Description       :
  7. **         Standart saver module for meshwriter.library.
  8. **         Saves the mesh as VRML 1 ascii file.
  9. **
  10. **
  11. **      Written by Stephan Bielmann
  12. **
  13. */
  14.  
  15. /*************************** Includes *******************************/
  16.  
  17. /*
  18. ** Amiga includes
  19. */
  20. #include <dos/dos.h>
  21. #include <dos/stdio.h>
  22.  
  23. #include <clib/dos_protos.h>
  24. #include <clib/alib_stdio_protos.h>
  25.  
  26. /*
  27. ** Project includes
  28. */
  29. #include "meshwriter_private.h"
  30.  
  31. /********************** Private functions ***************************/
  32.  
  33. /********************** Public functions ****************************/
  34.  
  35. /********************************************************************\
  36. *                                                                    *
  37. * Name         : write3VRML1                                         *
  38. *                                                                    *
  39. * Description  : Writes a standart VRML1 ascii file.                 *
  40. *                                                                    *
  41. * Arguments    : vrmlfile IN : An already opened file stream.        *
  42. *                mesh     IN : Pointer to the mesh.                  *
  43. *                                                                    *
  44. * Return Value : RCNOERROR                                           *
  45. *                RCWRITEDATA                                         *
  46. *                                                                    *
  47. * Comment      : Default material is the first.                      *
  48. *                                                                    *
  49. \********************************************************************/
  50. ULONG write3VRML1(BPTR vrmlfile, TOCLMesh *mesh) {
  51.     UBYTE                         buffer[200];
  52.     TOCLVertexNode                *ver=NULL;
  53.     TOCLMaterialNode            *mat=NULL;
  54.     TOCLPolygonNode             *pln=NULL;
  55.     TOCLPolygonsVerticesNode    *plv=NULL;
  56.     TOCLFloat                    r,g,b;
  57.             
  58.     /*
  59.     ** Write the header, the copyright, default separator and info node
  60.     */
  61.     if (FPuts(vrmlfile,"#VRML V1.0 ascii\n")!=DOSFALSE) return(RCWRITEDATA);
  62.     if (mesh->copyright) {
  63.         if (FPrintf(vrmlfile,"#\n#%s\n",mesh->copyright)==ENDSTREAMCH) return(RCWRITEDATA);      
  64.     }
  65.     if (FPrintf(vrmlfile,"\nDEF ROOT Separator {\n   Info {\n      string \"%s\"\n   }\n",mesh->name)==ENDSTREAMCH) return(RCWRITEDATA);
  66.  
  67.     /*
  68.     ** Write the light source
  69.     */
  70.     r=mesh->light.color.r;
  71.     g=mesh->light.color.g;
  72.     b=mesh->light.color.b;
  73.     r/=255,g/=255,b/=255;
  74.     sprintf(buffer,"   PointLight {\n      on TRUE\n      intensity  1.00\n      color %g %g %g\n      location  %g %g %G\n   }\n",
  75.             r,g,b,mesh->light.position.x,mesh->light.position.y,mesh->light.position.z);
  76.     if(FPuts(vrmlfile,buffer)!=DOSFALSE) return(RCWRITEDATA);
  77.  
  78.     /*
  79.     ** Write the vertices
  80.     */              
  81.     if(mesh->vertices.firstNode!=NULL) {
  82.         ver=mesh->vertices.firstNode;
  83.         if (FPuts(vrmlfile,"   Coordinate3 {\n      point [\n")!=DOSFALSE) return(RCWRITEDATA);
  84.         do {
  85.             TOCLVertex v=ver->vertex;
  86.             sprintf(buffer,"         %g %g %g,\n",v.x,v.y,v.z);
  87.             if(FPuts(vrmlfile,buffer)!=DOSFALSE) return(RCWRITEDATA);
  88.             ver=ver->next;
  89.         } while(ver!=NULL);
  90.         if (FPuts(vrmlfile,"      ]\n   }\n")!=DOSFALSE) return(RCWRITEDATA);
  91.     }
  92.     
  93.     /*
  94.     ** Write the materials
  95.     */
  96.     if(mesh->materials.firstNode!=NULL) {
  97.         if (FPuts(vrmlfile,"   Material {\n")!=DOSFALSE) return(RCWRITEDATA);
  98.           
  99.         /* The ambient color */
  100.         mat=mesh->materials.firstNode;
  101.         if (FPuts(vrmlfile,"      ambientColor [\n")!=DOSFALSE) return(RCWRITEDATA);
  102.         do {
  103.             TOCLColor col=mat->ambientColor;
  104.             r=col.r,g=col.g,b=col.b;
  105.             sprintf(buffer,"         %g %g %g,\n",r/255,g/255,b/255);
  106.             if (FPuts(vrmlfile,buffer)!=DOSFALSE) return(RCWRITEDATA);
  107.             mat=mat->next;
  108.         } while(mat!=NULL);
  109.         if (FPuts(vrmlfile,"      ]\n")!=DOSFALSE) return(RCWRITEDATA);
  110.  
  111.         /* The diffuse color */
  112.         mat=mesh->materials.firstNode;
  113.         if (FPuts(vrmlfile,"      diffuseColor [\n")!=DOSFALSE) return(RCWRITEDATA);
  114.         do {
  115.             TOCLColor col=mat->diffuseColor;
  116.             r=col.r,g=col.g,b=col.b;
  117.             sprintf(buffer,"         %g %g %g,\n",r/255,g/255,b/255);
  118.             if (FPuts(vrmlfile,buffer)!=DOSFALSE) return(RCWRITEDATA);
  119.             mat=mat->next;
  120.         } while(mat!=NULL);
  121.         if (FPuts(vrmlfile,"      ]\n")!=DOSFALSE) return(RCWRITEDATA);
  122.                 
  123.         /* The specular color */
  124.         mat=mesh->materials.firstNode;
  125.         if (FPuts(vrmlfile,"      specularColor [\n")!=DOSFALSE) return(RCWRITEDATA);
  126.         do {
  127.             if (FPuts(vrmlfile,"         0. 0. 0.,\n")!=DOSFALSE) return(RCWRITEDATA);
  128.                       
  129.             mat=mat->next;
  130.         } while(mat!=NULL);
  131.         if (FPuts(vrmlfile,"      ]\n")!=DOSFALSE) return(RCWRITEDATA);
  132.         
  133.         /* The emissive color */
  134.         mat=mesh->materials.firstNode;
  135.         if (FPuts(vrmlfile,"      emissiveColor [\n")!=DOSFALSE) return(RCWRITEDATA);
  136.         do {
  137.             if (FPuts(vrmlfile,"         0. 0. 0.,\n")!=DOSFALSE) return(RCWRITEDATA);
  138.           
  139.             mat=mat->next;
  140.         } while(mat!=NULL);
  141.         if (FPuts(vrmlfile,"      ]\n")!=DOSFALSE) return(RCWRITEDATA);
  142.         
  143.         /* The shininess */
  144.         mat=mesh->materials.firstNode;
  145.         if (FPuts(vrmlfile,"      shininess [\n")!=DOSFALSE) return(RCWRITEDATA);
  146.         do {
  147.             sprintf(buffer,"         %g,\n",mat->shininess);
  148.             if (FPuts(vrmlfile,buffer)!=DOSFALSE) return(RCWRITEDATA);
  149.             mat=mat->next;
  150.         } while(mat!=NULL);
  151.         if (FPuts(vrmlfile,"      ]\n")!=DOSFALSE) return(RCWRITEDATA);
  152.  
  153.         /* The transparency */
  154.         mat=mesh->materials.firstNode;
  155.         if (FPuts(vrmlfile,"      transparency [\n")!=DOSFALSE) return(RCWRITEDATA);
  156.         do {
  157.             sprintf(buffer,"         %g,\n",mat->transparency);
  158.             if (FPuts(vrmlfile,buffer)!=DOSFALSE) return(RCWRITEDATA);
  159.           
  160.             mat=mat->next;
  161.         } while(mat!=NULL);
  162.         if (FPuts(vrmlfile,"      ]\n")!=DOSFALSE) return(RCWRITEDATA);
  163.  
  164.         /*
  165.         ** End of material node
  166.         */            
  167.         if (FPuts(vrmlfile,"   }\n")!=DOSFALSE) return(RCWRITEDATA);
  168.  
  169.         /*
  170.         ** Set the material binding
  171.         */
  172.         if (FPuts(vrmlfile,"   MaterialBinding {\n      value PER_FACE_INDEXED\n   }\n")!=DOSFALSE) return(RCWRITEDATA);
  173.     }
  174.     
  175.     /*
  176.     ** Write the polygons
  177.     */
  178.       if(mesh->polygons.firstNode!=NULL) {             
  179.             pln=mesh->polygons.firstNode;
  180.         if (FPuts(vrmlfile,"   IndexedFaceSet {\n      coordIndex [\n")!=DOSFALSE) return(RCWRITEDATA);
  181.         do {                
  182.             if(pln->firstNode!=NULL) {  
  183.                 plv=pln->firstNode;
  184.                 if (FPuts(vrmlfile,"         ")!=DOSFALSE) return(RCWRITEDATA);
  185.                 do {
  186.                     if (FPrintf(vrmlfile,"%ld,",plv->vertexNode->index-1)==ENDSTREAMCH) return(RCWRITEDATA);
  187.  
  188.                     plv=plv->next;
  189.                 } while(plv!=NULL);
  190.                 if (FPuts(vrmlfile,"-1,\n")!=DOSFALSE) return(RCWRITEDATA);
  191.             }
  192.             pln=pln->next;
  193.         } while(pln!=NULL);
  194.         if (FPuts(vrmlfile,"      ]\n")!=DOSFALSE) return(RCWRITEDATA);
  195.         
  196.         /*
  197.         ** Write the material index, if there are some
  198.         ** Default material is 0.
  199.         */
  200.         if(mesh->materials.firstNode!=NULL) {
  201.             UBYTE i=0;
  202.             pln=mesh->polygons.firstNode;
  203.             if (FPuts(vrmlfile,"     materialIndex [\n         ")!=DOSFALSE) return(RCWRITEDATA);
  204.           
  205.             do {
  206.                 mat=pln->materialNode;
  207.                 
  208.                 if(mat!=NULL) {
  209.                     if (FPrintf(vrmlfile,"%ld,",mat->index-1)==ENDSTREAMCH) return(RCWRITEDATA);
  210.                 }
  211.                 else {
  212.                     if (FPuts(vrmlfile,"0,")!=DOSFALSE) return(RCWRITEDATA);
  213.                 }
  214.           
  215.                   if(++i==10) { 
  216.                     if (FPuts(vrmlfile,"\n         ")!=DOSFALSE) return(RCWRITEDATA);
  217.                     i=0;
  218.                 }
  219.                 pln=pln->next;
  220.             } while(pln!=NULL);
  221.         }
  222.         if (FPuts(vrmlfile,"\n      ]\n")!=DOSFALSE) return(RCWRITEDATA);
  223.     }
  224.     
  225.     /*
  226.     ** Write the end of the file
  227.     */
  228.     if (FPuts(vrmlfile,"   }\n}\n")!=DOSFALSE) return(RCWRITEDATA);
  229.  
  230.     return(RCNOERROR);
  231. }
  232.  
  233. /************************* End of file ******************************/
  234.